<template>
  <div class="message-page">
    <div class="content">
      <h3 style="line-height:60px">留言板</h3>
      <el-card>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="留言内容" prop="desc">
    <el-input type="textarea" v-model="ruleForm.desc" :rows="4"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')" size="mini" style="width:160px">立即留言</el-button>
    <el-button @click="resetForm('ruleForm')" size="mini" style="width:160px">重置</el-button>
  </el-form-item>
</el-form>
      </el-card>
      <el-card style="margin-top:20px">
        <h4>全部留言</h4>
        <div class="message">
          <div v-if="list.length==0" style="height:200px;line-height:200px;color:#ccc;">暂无留言~</div>
          <div class="message-item" v-for="(it,inx) in list" :key="inx">
            <img class="avator" :src="it.imageUrl" alt="">
            <div class="item-right">
              <span class="nickname">{{it.name}}：</span> {{it.content}} <span class="time"> {{it.messageDate}}</span>
            </div>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>
<script>
import {saveMessage,getMessageList} from '../../api'
export default {
  data(){
    return{
      list:[],
      ruleForm: {
          desc: ''
      },
      rules: {
          desc: [
            { required: true, message: '请填写留言', trigger: 'blur' }
          ]
        }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            const users = JSON.parse(localStorage.getItem('userinfo'))
            let form = {
              content:this.ruleForm.desc,
              imageUrl:users.imageUrl,
              name:users.workerName
            }
            saveMessage(form).then(res=>{
              if(res.code==200){
                this.getList();
                this.$message.success('留言成功')
                this.ruleForm={}
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      getList(){
        this.list=[]
        getMessageList(1).then(re=>{
          if(re.code==200&&re.data){
            re.data.forEach((it,inx)=>{
              if(inx%2==0){
                it.imageUrl='https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3911043214,3033548349&fm=26&gp=0.jpg'
              }else{
                it.imageUrl='https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=778124488,3055281860&fm=26&gp=0.jpg'
              }
              this.list.push(it)
            })
          }
        })
      }
  },
}
</script>
<style lang="scss" scoped>
.message-page{
  width: 100%;
  height: calc(100vh - 120px);
  overflow: auto;
  .content{
    width: 1000px;
    margin: 0 auto;
    text-align: center;
    .message{
      width: 100%;
      .message-item{
        width: 100%;
        display: flex;
        border:1px solid rgb(135, 225, 241);
        border-radius: 10px;
        background-color: #efefef;
        overflow: hidden;
        padding: 10px;
        margin-bottom: 15px;
      .avator{
        width: 80px;
        height: 80px;
      }
      .item-right{
        flex: 1;
        margin-left: 20px;
        text-align: left;
        .nickname{
          color: salmon;
        }
        .time{
          color: slateblue;
          opacity: 0.7;
          font-size: 12px;
          padding-left: 15px;
        }
      }
      }
    }
  }
}
</style>
